package net.flydy.ywxcx.wxcloudrun.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;


public class MyJSONTools {
    private static final ThreadLocal<ObjectMapper> om = new ThreadLocal<ObjectMapper>() {
        @Override
        protected ObjectMapper initialValue() {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            return objectMapper;
        }
    };

    public static Map<String, Object> toMap(Object bean) {
        Map<String, Object> map = om.get().convertValue(bean, new TypeReference<Map<String, Object>>() {
        });
        return map;
    }

    public static Map<String, Object> stringToMap(String jsonString) {
        Map<String, Object> map = JSONUtil.toBean(jsonString, Map.class);
        return map;
    }

    public static JSONObject stringToJson(String jsonString) {
       return JSONUtil.parseObj(jsonString);
    }

    public static List<Map<String, Object>> toList(Collection bean) {
        if (bean == null) {
            return null;
        }
       return om.get().convertValue(bean, new TypeReference<List<Map<String, Object>>>() {
        });
    }


    public static String toJsonStr(Object obj) throws Exception {
        return om.get().writeValueAsString(obj);
    }
    public static String toJsonStr2(Object obj)  {
        try {
            return om.get().writeValueAsString(obj);
        } catch (Exception e) {
            return "{}";
        }
    }

    public static <T> T jsonNode2Object(JsonNode jsonNode, Class<T> clazz) throws JsonProcessingException {
        return om.get().treeToValue(jsonNode, clazz);
    }

    public static JsonNode jsonStr2JsonNode(String jsonStr) throws Exception {
        return om.get().readTree(jsonStr);
    }

    public static <T> T jsonStr2Object(String jsonStr, Class<T> clazz) throws Exception {
        return jsonNode2Object(jsonStr2JsonNode(jsonStr), clazz);
    }

    public static JsonNode Object2JsonNode(Object obj) {
        return om.get().valueToTree(obj);
    }

    public static JsonNode inputStreamJsonNode(InputStream inputStream) throws Exception, IOException {
        return om.get().readTree(inputStream);
    }

    public static <T> T jsonStr2TypeReference(String jsonStr, TypeReference<T> valueTypeRef) throws Exception {
        return om.get().readValue(jsonStr, valueTypeRef);
    }


}
